<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    div {
      width: 200px;
      height: 200px;
      font-size: 30px;
      overflow: hidden;
    }
    .box1 {
      background: red;
    }
    .box2 {
      background: blue;
    }
    .box3 {
      background: yellow;
    }
  </style>
  <body>
    <div class="container">
      <button class="btn">按钮一</button>
      <button class="btn">按钮二</button>
      <button class="btn">按钮三</button>
      <div class="box1">1</div>
      <div class="box2">2</div>
      <div class="box3">3</div>
    </div>
    <script>
      var btns = document.querySelectorAll(".btn");
      var divs = document.querySelectorAll(".container div");
      btns.forEach((btn, k) => {
        btn.onclick = function () {
          console.log(k);
          divs.forEach((div, key) => {
            if (key == k) {
              div.style.display = "block";
            } else {
              div.style.display = "none";
            }
          });
        };
      });
    </script>
  </body>
</html>
